package com.geek.novel.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListAdapter;
import android.widget.TextView;
import com.geek.novel.entity.BookRead;
import com.geek.novel.ui.BookImageView;
import com.geek.novel.utils.BookUtil;
import com.geek.novel.utils.FormatUtil;
import com.geek.novel.utils.LogCatUtil;
import com.geek.novel.R;

import java.text.Format;
import java.util.List;

/**
 * 今日必读书籍的适配器
 */
public class TodayBookAdapter extends BaseAdapter {

    private List<BookRead> list;

    public TodayBookAdapter(List<BookRead> list){
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressLint("SetTextI18n")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        BookRead book = list.get(position);
        Context context = parent.getContext();
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.listview_book_today, parent, false);
        }
        BookImageView iv = convertView.findViewById(R.id.iv_today_book_image);
        BookUtil bookUtil = BookUtil.INSTANCE;
        String bookName = bookUtil.formatBookName(book.getBookName());
        String bookDesc = bookUtil.removeBookDescBlank(book.getBookDesc());

        String imgId = book.getImgId();
        iv.setBookName(bookName);
        iv.loadBookImage(imgId);

        //书名
        TextView tvBookName = convertView.findViewById(R.id.tv_today_book_name);
        tvBookName.setText(bookName);

        //书籍描述(替换掉空格)
        TextView tvBookDesc = convertView.findViewById(R.id.tv_today_book_desc);
        tvBookDesc.setText(bookDesc);

        //作者名
        String authorName = bookUtil.formatAuthorName(book.getAuthorName());

        //其他信息：作者 · 类型 · 连载 · 13万字
        TextView tvBookOtherInfo = convertView.findViewById(R.id.tv_today_book_other_info);
        StringBuilder otherInfo = new StringBuilder();
        otherInfo.append(authorName);
        if(!TextUtils.isEmpty(book.getCategoryStr())){
            otherInfo.append(" · ");
            otherInfo.append(book.getCategoryStr());
        }

        otherInfo.append(" · ");
        otherInfo.append(book.getBookStatus() == 0 ?"连载" :"完结");

        //字数
        long wordCount = book.getWordCount();
        if(wordCount > 0){
            otherInfo.append(" · ");
            String wordCountStr = FormatUtil.formatNumberCount(wordCount, 1);
            otherInfo.append(wordCountStr).append("字");
        }
        tvBookOtherInfo.setText(otherInfo.toString());
        return convertView;
    }

}
